package chtml

import (
	"github.com/microcosm-cc/bluemonday"
	"regexp"
)

// @title HtmlContentClean
// @description 清理内容
func HtmlContentClean(html string) string {
	p := GetNodeContentParser()
	html = p.Sanitize(html)
	return html
}

// @title HtmlPlainText
// @description 清理标题
func HtmlPlainText(title string) string {
	p := bluemonday.StrictPolicy()
	return p.Sanitize(title)
}

// UGCPolicy 复制一份，方便修改
func GetNodeContentParser() *bluemonday.Policy {

	p := bluemonday.NewPolicy()

	// dir / lang / id / title
	p.AllowStandardAttributes()

	p.AllowStandardURLs()

	p.AllowElements("article", "aside")

	p.AllowAttrs(
		"open",
	).Matching(regexp.MustCompile(`(?i)^(|open)$`)).OnElements("details")

	p.AllowElements("figure")

	p.AllowElements("section")

	p.AllowElements("summary")

	p.AllowElements("h1", "h2", "h3", "h4", "h5", "h6")

	p.AllowElements("hgroup")

	p.AllowAttrs("cite").OnElements("blockquote")

	p.AllowElements("br", "div", "hr", "p", "span", "wbr")

	p.AllowAttrs("href").OnElements("a")

	p.AllowAttrs("name").Matching(
		regexp.MustCompile(`^([\p{L}\p{N}_-]+)$`),
	).OnElements("map")

	p.AllowAttrs("alt").Matching(bluemonday.Paragraph).OnElements("area")

	p.AllowAttrs("coords").Matching(
		regexp.MustCompile(`^([0-9]+,)+[0-9]+$`),
	).OnElements("area")

	p.AllowAttrs("href").OnElements("area")

	p.AllowAttrs("rel").Matching(bluemonday.SpaceSeparatedTokens).OnElements("area")

	p.AllowAttrs("shape").Matching(
		regexp.MustCompile(`(?i)^(default|circle|rect|poly)$`),
	).OnElements("area")

	p.AllowAttrs("usemap").Matching(
		regexp.MustCompile(`(?i)^#[\p{L}\p{N}_-]+$`),
	).OnElements("img")

	p.AllowElements("abbr", "acronym", "cite", "code", "dfn", "em",
		"figcaption", "mark", "s", "samp", "strong", "sub", "sup", "var")

	p.AllowAttrs("cite").OnElements("q")

	// "time" is permitted
	p.AllowAttrs("datetime").Matching(bluemonday.ISO8601).OnElements("time")

	////////////////////
	// Style elements //
	////////////////////

	// block and inline elements that impart no semantic meaning but style the
	// document
	p.AllowElements("b", "i", "pre", "small", "strike", "tt", "u")

	// "style" is not permitted as we are not yet sanitising CSS and it is an
	// XSS attack vector

	//////////////////////
	// HTML5 Formatting //
	//////////////////////

	// "bdi" "bdo" are permitted
	p.AllowAttrs("dir").Matching(bluemonday.Direction).OnElements("bdi", "bdo")

	// "rp" "rt" "ruby" are permitted
	p.AllowElements("rp", "rt", "ruby")

	///////////////////////////
	// HTML5 Change tracking //
	///////////////////////////

	// "del" "ins" are permitted
	p.AllowAttrs("cite").Matching(bluemonday.Paragraph).OnElements("del", "ins")
	p.AllowAttrs("datetime").Matching(bluemonday.ISO8601).OnElements("del", "ins")

	///////////
	// Lists //
	///////////

	p.AllowLists()

	////////////
	// Tables //
	////////////

	p.AllowTables()

	p.AllowAttrs(
		"value",
		"min",
		"max",
		"low",
		"high",
		"optimum",
	).Matching(bluemonday.Number).OnElements("meter")

	// "progress" is permitted
	p.AllowAttrs("value", "max").Matching(bluemonday.Number).OnElements("progress")

	//////////////////////
	// Embedded content //
	//////////////////////

	// Vast majority not permitted
	// "audio" "canvas" "embed" "iframe" "object" "param" "source" "svg" "track"
	// "video" are all not permitted

	p.AllowImages()

	// 允许颜色 color
	p.AllowStyles("color").Matching(regexp.MustCompile("(?i)^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$")).Globally()
	p.AllowStyles("background-color").Matching(regexp.MustCompile("(?i)^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$")).Globally()
	p.AllowStyles("text-align").Matching(regexp.MustCompile("(?i)^(center|left|right)$")).Globally()
	p.AllowStyles("text-decoration").Matching(regexp.MustCompile("(?i)^(underline|text-decoration|overline|blink|)$")).Globally()

	return p
}
